package com.aorise.service.approve.impl;

import com.aorise.exception.ServiceException;
import com.aorise.mapper.approve.*;
import com.aorise.mapper.halt.HaltInformMapper;
import com.aorise.mapper.halt.HaltMapper;
import com.aorise.mapper.migration.MigrationInformMapper;
import com.aorise.mapper.migration.MigrationMapper;
import com.aorise.model.approve.*;
import com.aorise.service.approve.ApproveService;
import com.aorise.utils.ConstDefine;
import com.aorise.utils.page.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:cat
 * @Desicription: 审批信息接口服务实现类
 * @Date:Created in 2019-06-17
 * @Modified By:
 */
@Service
public class ApproveServiceImpl implements ApproveService {

    @Autowired
    private ApproveMapper approveMapper;
    @Autowired
    private HaltMapper haltMapper;
    @Autowired
    private MigrationMapper migrationMapper;
    @Autowired
    private HaltInformMapper haltInformMapper;
    @Autowired
    private MigrationInformMapper migrationInformMapper;
    /**
     * @Author:cat
     * @Description 分页查询审批信息
     * @params: map 查询条件
     * @params: page 分页
     * @Date:2019-06-17
     * @Return: List<ApproveModel> 审批信息集合
     * @Modified By:
     */
    @Override
    public List<ApproveModel> getAllApproveListByPage(Map<String, Object> map, Page page) {
        List<ApproveModel> approveModels = null;

        /**查询总条数*/
        Long count = approveMapper.getAllApproveListTotalByPage(map);
        if (count > 0) {
            page.setTotalCount(count);
            page.setTotalPage(count, page.getEveryPage());
            if (page.getCurrentPage() > 0 && page.getEveryPage() > 0) {
                /**查询分页条数*/
                approveModels = approveMapper.getAllApproveListByPage(map);
            }
        } else {
            page.setTotalCount(0L);
        }
        return approveModels;
    }

    /**
     * @Author:cat
     * @Description 审批计划停机、点位迁建申请
     * @params: map 审批信息
     * @Date:2019-06-17
     * @Return: Integer 主键
     * @Modified By:
     */
    @Override
    @Transactional
    public Integer approveHaltMigration(Map<String, Object> map) {
        if(map.get("type").equals(ConstDefine.APPROVE_TYPE_HALT)){
            /**计划停机*/
            map.put("approveTime", getCurrentTime("yyyy-MM-dd HH:mm:ss"));
            int iRet = haltMapper.updateHalt(map);
            if(iRet>0){
                Map<String, Object> mapAdd = new HashMap<>();
                mapAdd.put("haltId",map.get("id"));
                mapAdd.put("userId",map.get("createUser"));
                haltInformMapper.addHaltInform(mapAdd);
            }
            return iRet;
        }else {
            /**点位迁建*/
            map.put("approveTime", getCurrentTime("yyyy-MM-dd HH:mm:ss"));
            int iRet =  migrationMapper.updateMigration(map);
            if(iRet>0){
                Map<String, Object> mapAdd = new HashMap<>();
                mapAdd.put("migrationId",map.get("id"));
                mapAdd.put("userId",map.get("createUser"));
                migrationInformMapper.addMigrationInform(mapAdd);
            }
            return iRet;
        }
    }

    /**
     * 获取当前时间
     * @return 返回当前时间字符串
     */
    public static String getCurrentTime(String format){
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.format(new Date()).toString();
    }

    /**
     * @Author:cat
     * @Description 查询审批中心待审批消息总数
     * @Date:2019-06-19
     * @Return: Long 数据总条数
     * @Modified By:
     */
    @Override
    public Long getApproveMsgNum() {
        Map<String, Object> map = new HashMap<>();
        map.put("status",ConstDefine.APPROVE_STATUS_WAIT);
        map.put("state",ConstDefine.STATE_ABLE);

        Long hSum = haltMapper.getHaltSum(map);
        Long mSum = migrationMapper.getMigrationSum(map);

        return hSum+mSum;
    }
}
